<?php

define('MANAGE_PATH', '../addons/mx_shop/static/manage/');

function mrolesUrl($do = '', $query = null, $full = false) {
    global $_W, $_GPC;
    //$do = 'merch.manage.'.$do;
    $dos = explode('/', trim($do));
    $routes = array('mroles');
    $routes[] = $dos[0];
    if (isset($dos[1])) {
        $routes[] = $dos[1];
    }
    if (isset($dos[2])) {
        $routes[] = $dos[2];
    }
    if (isset($dos[3])) {
        $routes[] = $dos[3];
    }
    $r = implode(".", $routes);

    if(!is_array($query)){
        $query = array();
    }
    if (!empty($r)) {
        $query = array_merge(array('r' => $r), $query);
    }
    $query = array_merge(array('do' => 'web'), $query);
    $query = array_merge(array('m' => "mx_shop"), $query);
    return str_replace("./index.php", "./mroles.php", wurl('site/entry', $query) );

}

function owpager($total, $pindex, $psize, $params = array(), $num=3){

    global $_W;

    $pagenum = ceil($total / $psize);
    if($pagenum < 2){
        return '';
    }
    $url = $_W['script_name'] . '?' . http_build_query($_GET);

    $url = str_replace(strchr($url, '&page='), '', $url);
    if(!empty($params) && is_array($params)){
        foreach ($params as $pa=>$ra){
            $url .= '&'.$pa.'='.$ra;
        }
    }

    $html = '<div class="bottomPage">';
    $html .= '<div class="pager-nav">';
    $html .= '<ul id="pageList">';
    $html .= '<li><a>总计'.$total.'条 共'.$pagenum.'页</a></li>';
    if($pindex > 1){
        $html .= '<li class="disabled pager-prev"><a href="'.$url.'&page=1">首页</a></li>';
        $html .= '<li class="disabled pager-prev"><a href="'.$url.'&page='.($pindex-1).'">上一页</a></li>';
    }

    for($i=0; $i<$num; ++$i){
        if($pindex - ($num-$i) > 0){
            $html .= '<li><a href="'.$url.'&page='.($pindex-($num-$i)).'">'.($pindex-($num-$i)).'</a></li>';
        }
    }

    $html .= '<li class="active"><span>'.$pindex.'</span></li>';

    for($i=1; $i<=$num; ++$i){
        if($pindex + $i <= $pagenum){
            $html .= '<li><a href="'.$url.'&page='.($pindex+$i).'">'.($pindex+$i).'</a></li>';
        }
    }

    if($pagenum > $pindex){
        $html .= '<li class="pager-next"><a href="'.$url.'&page='.($pindex+1).'">下一页</a></li>';
        $html .= '<li class="pager-next"><a href="'.$url.'&page='.($pagenum).'">尾页</a></li>';
    }
    $html .= '</ul></div></div>';

    return $html;

}

/*
    * 保存登录信息
    * @param array $account 账号信息
    * return $account
    */
function getRole($account){

    global $_W,$_GPC;

    //威赢家
    $owner = pdo_fetch('SELECT id FROM '.tablename('mx_shop_service_goods_owner').' WHERE uid=:uid AND deleted=0 LIMIT 1', array(':uid'=>$account['id']));
    $account['isowner'] = empty($owner) ? false : true;

    //店主
    $manage = pdo_fetch('SELECT id FROM '.tablename('mx_shop_mendian').' WHERE manageid=:manageid AND deleted=0 LIMIT 1', array(':manageid'=>$account['id']));
    $account['ismanage'] = empty($manage) ? false : true;

    //服务商
    $channel = pdo_fetch('SELECT id FROM '.tablename('mx_shop_mendian').' WHERE channelid=:channelid AND deleted=0 LIMIT 1', array(':channelid'=>$account['id']));
    $account['ischannel'] = empty($channel) ? false : true;

    //运营商
    $operator = pdo_fetch('SELECT id FROM '.tablename('mx_shop_operator').' WHERE uid=:uid AND deleted=0 LIMIT 1', array(':uid'=>$account['id']));
    $account['isoperator'] = empty($operator) ? false : true;

    return $account;
}

global $_W;
$routes = explode('.', $_W['routes'] );
$GLOBALS['_W']['tab'] = isset($routes[2])?$routes[2]:'';
$uniacid = intval($_GPC['__uniacid']);
$session = $_SESSION['__mroles_uniacid'];
if(!empty($session)){
    $uniacid = $session;
}
/*if($_W['routes']!='mroles.login'){
    $session_key = '__mroles_'.$uniacid.'_session';
    $session = json_decode(base64_decode($_GPC[$session_key]), true);

    if(is_array($session)) {

        $account = pdo_fetch("select id,mobile,pwd,salt from ".tablename('mx_shop_user')." where id=:id limit 1",array(':id'=>$session['id']));
        if(!is_array($account)  || $session['hash'] != md5($account['pwd'] . $account['salt'])) {
            isetcookie($session_key, false, -100);
            header('location: '.mrolesUrl('login', array('i'=>$uniacid)));
            exit;
        }

        $res = getRole($account);
        if(!$res['isowner'] && !$res['ismanage'] && !$res['ischannel'] && !$res['isoperator']){
            isetcookie($session_key, false, -100);
            header('location: '.mrolesUrl('login', array('i'=>$uniacid)));
            exit;
        }

        $GLOBALS['_W']['uniaccount'] = $account;
    } else{

        isetcookie($session_key, false, -100);
        header('location: '.mrolesUrl('login', array('i'=>$uniacid)));
        exit;
    }

}*/
$GLOBALS['_W']['uniacid'] = $uniacid;
$GLOBALS['_W']['isowner'] = $session['isowner'];
$GLOBALS['_W']['ismanage'] = $session['ismanage'];
$GLOBALS['_W']['ischannel'] = $session['ischannel'];
$GLOBALS['_W']['isoperator'] = $session['isoperator'];
$GLOBALS['_W']['manageid'] = $session['id'];
$mroles_user = pdo_fetch("select realname from ".tablename('mx_shop_user')." where id=:id limit 1",array(':id'=>$session['id']));
$GLOBALS['_W']['manage_realname'] = $mroles_user['realname'];

unset($mroles_user);

load()->func('tpl');